package com.example.adouble.cehuamenu.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * 对 RecyclerView.Adapter 做了一个简单的封装
 * @param <T>
 */
public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<BaseRecyclerAdapter.BaseViewHolder> {

    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;

    //上下文环境
    private Context context;
    //数据集合
    private List<T> data;

    /**
     * @param context
     * @param data
     */
    public BaseRecyclerAdapter(Context context, List<T> data) {
        this.context = context;
        this.data = data;
    }


    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(getItemLayoutId(), parent, false);
        return new BaseViewHolder(view);
    }

    /**
     * 此处有巨坑
     * 该方法自动生成的话第一个参数类型是 BaseViewHolder
     * 要改为 BaseRecyclerAdapter.BaseViewHolder
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(BaseRecyclerAdapter.BaseViewHolder holder, final int position) {
        T t = data.get(position);
        holder.setT(t);
        bindData(holder, t);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    /**
     * 将数据绑定到 item 视图上
     * @param holder
     * @param t
     */
    public abstract void bindData(BaseViewHolder holder, T t);

    /**
     * 子类通过重写此方法设置 RecyclerView 的 item 项的布局视图
     * @return
     */
    public abstract int getItemLayoutId();

    /**
     * 刷新数据
     * @param data
     */
    public void refresh(List<T> data){
        this.data = data;
        notifyDataSetChanged();
    }

    /**
     * 添加数据
     * @param data
     */
    public void addData(List<T> data){
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    /**
     * RecyclerView item 项的点击事件回调接口
     *
     * @param <T>
     */
    public interface OnItemClickListener<T> {

        void onItemClick(View v, T t);
    }

    /**
     * RecyclerView item 项的长按事件回调接口
     *
     * @param <T>
     */
    public interface OnItemLongClickListener<T> {

        void onItemLongClick(View v, T t);
    }

    public void setOnItemClickListener(OnItemClickListener<T> onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener<T> onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    /**
     * 封装 ViewHolder
     */
    class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

        // item 中的控件缓存在这里
        private SparseArray<View> views;
        // 数据
        private T t;

        public BaseViewHolder(View itemView) {
            super(itemView);
            views = new SparseArray<>();

            configListener();
        }

        public void setT(T t) {
            this.t = t;
        }

        /**
         * 获取 itemView 中的控件
         *
         * @param id 控件id
         * @return
         */
        public View getView(int id) {
            View view = views.get(id);
            if (view == null) {
                view = itemView.findViewById(id);
                views.put(id, view);
            }
            return view;
        }

        /**
         * 配置监听器
         */
        public void configListener() {
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(v, t);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (onItemLongClickListener != null) {
                onItemLongClickListener.onItemLongClick(v, t);
            }
            return false;
        }
    }

}
